﻿namespace Compression.ImageCompressor
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.IO.Compression;

    public class FileWorker:IDisposable
    {
        FileStream file = null;
        BufferedStream buffer = null;
        BinaryReader reader = null;
        BinaryWriter writer = null;
        char[] temp = new char[10000];
        int top = 0;
        int tail = 0;

        public FileWorker(string filename,bool readaccess)
        {
            file = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            buffer = new BufferedStream(file, 10000);
            if (readaccess == true)
            {
                DeflateStream deflate = new DeflateStream(buffer, CompressionMode.Decompress);                
                //GZipStream deflate = new GZipStream(buffer, CompressionMode.Decompress);
                reader = new BinaryReader(deflate,Encoding.Unicode);
            }
            else
            {
                DeflateStream deflate = new DeflateStream(buffer, CompressionMode.Compress);
                //GZipStream deflate = new GZipStream(buffer, CompressionMode.Compress);
                writer = new BinaryWriter(deflate, Encoding.Unicode);
            }
        }

        public Int16 ReadInt16()
        {
            return reader.ReadInt16();
        }

        public UInt16 ReadUInt16()
        {
            return reader.ReadUInt16();
        }

        public Int32 ReadInt32()
        {
            return reader.ReadInt32();
        }

        public UInt32 ReadUInt32()
        {
            return reader.ReadUInt32();
        }

        public Boolean ReadBoolean()
        {
            return reader.ReadBoolean();
        }
        
        public void Write(bool val)
        {
            writer.Write(val);
        }

        public void Write(UInt16 val)
        {
            writer.Write(val);
        }

        public void Write(Int16 val)
        {
            writer.Write(val);
        }

        public void Write(int val)
        {
            writer.Write(val);
        }

        public void Write(UInt32 val)
        {
            writer.Write(val);
        }

        #region Члены IDisposable

        public void Dispose()
        {
            if (reader != null)
            {
                reader.Close();
                
                //reader.Dispose();
            }
            if (writer != null)
            {
                writer.Close();
                //writer.Dispose();
            }
            if (buffer != null)
            {
                buffer.Close();
                buffer.Dispose();
            }
            if (file != null)
            {
                file.Close();
                file.Dispose();
            }
        }

        #endregion
    }
}
